﻿#include "itemadd.h"
#include "ui_itemadd.h"

ItemAdd::ItemAdd(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ItemAdd)
{
    ui->setupUi(this);
}

ItemAdd::~ItemAdd()
{
    delete ui;
}

bool ItemAdd::addItem(const QString &category, const QString &label, const QString &detail, const QString &addition)
{
    QSqlQuery query;
    query.prepare(QString("insert into %1 (category,label,detail,addition) values (?,?,?,?)").arg(TD_PRODUCT));
    query.addBindValue(category);
    query.addBindValue(label);
    query.addBindValue(detail);
    query.addBindValue(addition);
    return query.exec();
}

void ItemAdd::on_btn_submit_clicked()
{
    QString category=ui->category->text().trimmed();
    if(category.isEmpty())
        return;

    QString label=ui->label->text().trimmed();
    if(label.isEmpty())
        return;

    QString detail=ui->detail->toPlainText();
    if(detail.isEmpty())
        return;

    QString addition=ui->addition->toPlainText();

    if(!addItem(category,label,detail,addition))
        QMessageBox::warning(this,QStringLiteral("异常情况"),QStringLiteral("无法提交数据！"));
    else
        this->accept();
}
